<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>闭包和作用域</title>
</head>
<body>
<script>
    {
//        var i = 10;

        function fn() {
            var i = 10; //11  12
            return function (n) { //n =10 20

                console.log(n + (++i)) // 20 + (11)  20 + (12)

            }
        }

        f = fn();//{}
        /*f= function (n) {
            console.log(n + (++i))
        }*/
        f(10); //21
        f(20); //32
        fn()(10); //21
        fn()(20); //20  + 11  31
    }

    {
        function foo(i){
            return function (n){
                console.log(n + i++);
            }
        }
        f = foo(3);
        f(4);
        f(5);
        foo(1)(2);
        foo(2)(3);
    }

    {
        var number = 2; //4  8
        var obj = {
            number: 4, //8
            fn1: (function () {
                this.number *= 2;
                number = number *2; //NaN
                var number = 3;//3  9
                return function () {
                    console.log(this);
                    this.number *= 2; //window
                    number *= 3;
                    alert("fn内"+number); //9  27
                }
            })()
        };
        var fn1 = obj.fn1;  //this-->window
        alert("全局"+number); //4
        fn1(); //9
        obj.fn1(); //obj 27
        alert("window.number全局"+window.number);
        alert("obj.number全局"+obj.number);


        //4  9  27 8  8
    }
    {
        /*var age = 20;
        var obj = {
            age : 18,
            say: ()=>{
                console.log(this.age)
            }
        };

        obj.say();

        /!*()=>{
            console.log(this.age)
        }*!/*/
    }

</script>
</body>
</html>